$(document).ready(function () { 
	$('.form_date1').datetimepicker({
		weekStart: 1,
		todayBtn: 1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		clearBtn:true
	});
	var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				
				onClick: zTreeOnCheck
			}
		};
	
	
	var mzjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_zq"});
	loadSelect("select[name=mz]",mzjson.rows,'id','name');
	
	var gjjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_gj"});
	loadSelect("select[name=gj]",gjjson.rows,'id','name');
	
	var gjjson = ajaxResult("get",false,"../../mbxtglbasetableservlet.getPullData.do",{tn:"mb_gy_zjlx"});
	loadSelect("select[name=zjlx]",gjjson.rows,'id','name');
	
	var jgjson = ajaxResult("get",false,"../../mbxtgljgservlet.getDataTwoJg.do");	
	loadSelect("select[name=szjg]",jgjson.data,'zdbh','jgmc');
	var jsonTree = ajaxResult("get",false,"../../mbxtgljgservlet.getDataByTree.do");	
	var params =$("#form_bd1").serializeObject();
	createTable('#userInfo','../../mbxtglyhxxservlet.getData.do','zdbh',tblColTitle,null,null);
	$.fn.zTree.init($("#treeDemo"), setting, jsonTree.data);

	$(".newAdd").click(function(){
		var sort = ajaxResult("get",false,"../../mbxtglyhxxservlet.getMaxSort.do");
		$("#sorted").val(sort.max);
	})
	
	$("#query_btn").click(function(){
		refreshTable("#userInfo","#form_bd");
	});
	
	$("#baocun").click(function(){
		if(!checkform("#form_bds")){
			return;
		}
		 
		var params =$("#form_bds").serializeObject();	
		 if(params.dh!=""){
			if(!checkphone($("#form_bds #dh"))){
				return;
			}
		  }
		  if(params.dzyj!=""){
			  if(!checkemail($("#form_bds #dzyj"))){
				  return;
			  }
		  }
		var data = ajaxResult("post",false,"../../mbxtglyhxxservlet.insert.do",params);
		 if(data.status="OK"){
			 alertFunc("添加成功");
			 $("#form_bds")[0].reset();
		 }else{
			 alertFunc("添加失败")
		 }
			refreshTable("#userInfo","#form_bd");
			$('#exampleModal').modal('hide');
	});
	$("#xiugai").click(function(){
		 if(!checkform("#form_bds2")){
			  return;
		  }
		 
		var params =$("#form_bds2").serializeObject();	
		 if(params.dh!=""){
			if(!checkphone($("#form_bds2 #dh"))){
				return;
			}
		  }
		  if(params.dzyj!=""){
			  if(!checkemail($("#form_bds2 #dzyj"))){
				  return;
			  }
		  }
		var data = ajaxResult("get",false,"../../mbxtglyhxxservlet.update.do",params);
		 if(data.status="OK"){
			 alertFunc("修改成功");
		 }else{
			 alertFunc("修改失败")
		 }
		 refreshTableAtCurPage("#userInfo","#form_bd");
			$('#exampleModal2').modal('hide');
	});
	
	
});

var log, className = "dark";
function zTreeOnCheck(event, treeId, treeNode) {
	if(treeNode.level==0){
		return;
	}
	var params ={
		 pageNumber:1,
		 limit:pageSize,
		 offset:0,
		 szjg:treeNode.id
	 }
	$("#userInfo").bootstrapTable('refresh',{query:params});	
};



function selectInfo(zdbh){
	var json = ajaxResult("get",false,"../../mbxtglyhxxservlet.getDataById.do",{zdbh:zdbh});
	loadData("#form_bds1",json.rows);
}
function selectInfoTOupdate(zdbh){
	var json = ajaxResult("get",false,"../../mbxtglyhxxservlet.getDataById.do",{zdbh:zdbh});
	loadData("#form_bds2",json.rows);
	$("#zdbh").val(zdbh);
}

var tblColTitle = [ 
	{ title: '序号', width: 10, align: "center", formatter: function (value, row, index) { return index + 1; } },
	{ title: '姓名', field: 'xm' ,width: 50},
	{ title: '证件类型', field: 'zjlxName' ,width: 95},
	{ title: '证件号码', field: 'zjhm' ,width: 95},
	{ title: '性别', field: 'xb' ,width: 10,formatter: function (value, row, index) { 
		var result="";
		if(row.xb==1){
			result="男";
		}else{
			result="女";
		}
		return result;
	}},
	{ title: '国籍', field: 'gjName' ,width: 55},
	{ title: '民族', field: 'zqName' ,width: 70},
	{ title: '出生日期', field: 'csrq' ,width: 95},
	{ title: '电话', field: 'dh' ,width: 95},
	{ title: '电子邮件', field: 'dzyj' ,width: 95},
	{ title: '操作', field: '' ,width: 220, formatter: function (value, row, index) { 
		var result="";
		result="<a  href='#' onclick='selectInfo("+row.zdbh+")' data-toggle='modal' data-target='#exampleModal1' >查看</a>&nbsp;&nbsp;&nbsp;"+
		"<a href='#' onclick='selectInfoTOupdate("+row.zdbh+")' data-toggle='modal' data-target='#exampleModal2' >修改</a>&nbsp;&nbsp;&nbsp;"+
		"<a href='#' onclick='tableJs("+row.zdbh+")' data-toggle= 'modal'  data-target='#exampleModal6' >授权</a> &nbsp;&nbsp;&nbsp;"+
		"<a href='#' onclick='selectJs("+row.zdbh+")' data-toggle= 'modal'  data-target='#exampleModal5' >查看权限</a> &nbsp;&nbsp;&nbsp;"+
		"<a href='#' onclick='zhuxiao("+row.zdbh+")' data-toggle= 'modal'  data-target='#exampleModal7' >注销</a> ";
	return result;
	
	} }
];
var userID= '';
function tableJs(zdbh){
	userID=zdbh;
	$("#userdb").bootstrapTable('destroy');
	createTable('#userdb','../../mbxtglqxservlet.getJsListByPerson.do','zdbh',us,{"zdbh":zdbh,"sfwzj":0},null);
}
function selectJs(zdbh){
	$("#jsdb").bootstrapTable('destroy');
	queryTableNoPage('#jsdb','../../mbxtglzjservlet.getjs.do',jsColTitle,{userid:zdbh});
}
function zhuxiao(zdbh){
	window.top.bootbox.setLocale("zh_CN");
	window.top.bootbox.confirm("确认注销该用户吗？",function(r){
		if(r){
			var json = ajaxResult("get",false,"../../mbxtglyhxxservlet.delete.do",{zdbh:zdbh});
			if(json.status=="OK"){
				alertFunc("注销成功");
				refreshTableAtCurPage("#userInfo","#form_bd");
			}else{
				alertFunc("注销失败");
			}
		}
	});
}
var us = [
	{ title: '角色名称', field: 'jsmc' },
	{ title: '角色描述', field: 'jsms' ,
 	   formatter: function(value, row, index) {
	        	var result="";	 
	        	if(row.jsms!=null && row.jsms!=""){
	        	if(row.jsms.length>27){
	        		result ="<span title='"+row.jsms+"'>"+row.jsms.substring(0,27)+"...</span>";  	       	        		
	        	}else{
	        		result =row.jsms;
	        	}
	        	}
       	return result;
	      } },
	{ title: '授权', field: '', formatter: function (value, row, index) {
		 var result="";
		 var ischeck = row.rybm;
		 if(ischeck!=null&&ischeck!=""){
			result="<input type='radio' checked=true name='radio-"+row.zdbh+"' onclick='fenpei("+JSON.stringify(row.zdbh)+")'>";
		 }else{
			result="<input type='radio' name='radio-"+row.zdbh+"' onclick='fenpei("+JSON.stringify(row.zdbh)+")'>";
		 }
		 //result="<a href='#' onclick='fenpei("+JSON.stringify(row.jsbm)+")'>分配</a>&nbsp;&nbsp;&nbsp;";
		 return result;
	}}
];

function refreshTable(tableid,formid){	 
	var params =$(formid).serializeObject();		  
	params["pageNumber"] = 1;
	params["limit"] = pageSize;
	params["offset"] = 0;	 
	$(tableid).bootstrapTable('refresh',{query:params});	
};

function fenpei(zdbh){
	var a= $("#userInfo").bootstrapTable('getSelections');
	var json = ajaxResult("get",false,"../../mbxtglzjservlet.assignJs.do",{userid:userID,roleid:zdbh});
	if(json.status=="OK"){
		//alertFunc("分配成功");
		//$('#exampleModal6').modal('hide');
	}else{
		//alertFunc("分配失败");
		//$('#exampleModal6').modal('hide');
	}
	userID="";
	//refreshTableAtCurPage("#userInfo","#form_bd");
}


var jsColTitle = [ 
	{ title: '角色编码', field: 'jsbm' },
	//{ title: '角色状态', field: 'jszt' },
	{ title: '角色名称', field: 'jsmc' },
	{ title: '角色描述', field: 'jsms' }
];
